<template>
  <div style="background: #ececec; padding: 30px">
    <a-card type="inner" v-if="designDemand.spaceQuestionList.length != 0" v-for="(designDemand,index) in this.designDemandList" :title="designDemand.spaceName"
            :bordered="false" style=";width: 100%" :key="index">
      <p v-for="(spaceQuestion,index1) in designDemand.spaceQuestionList" :key="index1">
        <span style="font-weight: bold">{{spaceQuestion.questionName}}</span><br><br>

        <a-radio-group v-if="spaceQuestion.questionType == 1" v-model="spaceQuestion.questionValue" @change="changeValue(spaceQuestion.questionKey,spaceQuestion.questionValue)">
          <a-radio :value="questionOption" v-for="(questionOption,index3) in spaceQuestion.questionOptionsList" :key="index3">
            {{questionOption}}
          </a-radio>
        </a-radio-group>

        <a-checkbox-group v-if="spaceQuestion.questionType == 2" v-model="spaceQuestion.questionValues" @change="changeOptions(spaceQuestion.questionKey,spaceQuestion.questionValues)">
          <a-checkbox :value="questionOption" v-for="(questionOption,index4) in spaceQuestion.questionOptionsList" :key="index4">
            {{questionOption}}
          </a-checkbox>
        </a-checkbox-group>
        <!--<a-checkbox-group v-show="spaceQuestion.questionType == 2"
          v-model="spaceQuestion.questionValue"
          name="checkboxgroup"
          :options="spaceQuestion.questionOptionsList"
                          @change="changeOptions(spaceQuestion.questionKey,spaceQuestion.questionValue)"
        />-->
      </p>
    </a-card>

    <br>
    <a-button type="primary" @click="saveDesignDemand">保存</a-button>
  </div>
</template>

<script>

import {ajaxGetDictItems, getDictItemsFromCache} from '@/api/api'
import {httpAction, getAction, postAction} from '@api/manage'
import designDemandList from "../../designdemandsetting/designDemandList";

export default {
  name: "designDemand",
  comments: {},
  props: {
    record: {}
  },
  data() {
    return {
      spaceQuestion: {},
      dictCode: 'customer_info_stage_type',
      model: {},
      designDemandList: [],
      keys: [],
      url: {
        designDemandListUrl: '/ghDesignDemandType/queryList',
        saveDesignDemandUrl: '/ghCustomerInfo/saveDesignDemand',
        customerDesignDemandUrl: '/ghCustomerInfo/findDesignDemandByCustomerId'
      },
    }
  },
  methods: {
    initDesignDemandData() {
      let params = {};
      params.customerId = this.record.id;
      getAction(this.url.designDemandListUrl,params).then((res) => {
        if (res.success) {
          this.designDemandList = res.result;
          /*for (let i = 0; i < this.designDemandList.length; i++) {
            for (let j = 0; j < this.designDemandList[i].keyList.length; j++) {
              this.keys.push(this.designDemandList[i].keyList[j]);
            }
          }*/
        }
      })
    },
    saveDesignDemand() {
      let params = {};
      params.customerId = this.record.id; //客户id
      params.designDemandJson = JSON.stringify(this.model);
      postAction(this.url.saveDesignDemandUrl, params).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    changeValue(key, value) {
      if (this.model.hasOwnProperty(key)){
        delete this.model[key];
        this.model[key] = value;
      }else {
        this.model[key] = value;
      }
    },
    changeOptions(key,arr){
      if (this.model.hasOwnProperty(key)){
        //delete this.model[key];
        this.model[key] = arr;
      }else {
        this.model[key] = arr;
      }
    },
  },
  created() {
    this.initDesignDemandData();
  },
}
</script>

<style scoped>

</style>